<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Train as ModelTrain;
use app\common\model\train\Inquiry;
use fast\Http;
use GuzzleHttp\Client;
use think\Validate;

/**
 * 教育接口
 */
class Train extends Api
{
    protected $noNeedLogin = ['lists', 'info'];
    protected $noNeedRight = '*';
    /**
     * @Author LK
     * @ApiTitle (教育列表)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $list = ModelTrain::all();
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (配置详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     * 
     */
    public function info()
    {
        $param = request()->param();
        extract($param);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $data = ModelTrain::get($id);
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (申报)
     * @ApiMethod (POST)
     * @ApiParams (name="t_id", type="integer", required=true, sample="", description="申报ID")
     * @ApiParams (name="data", type="integer", required=true, sample="", description="提交数据")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function declaration()
    {
        $param = request()->param();
        extract($param);
        $validate = new Validate([
            'data'   => 'require',
            't_id'  => 'require',
        ], [
            'data.require'  => '数据不能为空',
            't_id.require' => '参数错误',
        ]);
        $result = $validate->check($param);
        if (!$result) {
            $this->error(__($validate->getError()));
        }
        $param['text'] = json_encode($data);
        $param['user_id'] = $this->auth->id;
        $model = new Inquiry();
        $res = $model->allowField(true)->save($param);
        if ($res) {
            $this->success('提交成功');
        }
        $this->error('提交失败');
    }

    /**
     * @Author LK
     * @ApiTitle (表单查询)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function apply()
    {
        $param = request()->param();
        extract($param);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $train = ModelTrain::get($id);
        if (!$train) {
            $this->error('查询失败');
        }
        if (!$train->applicationId || !$train->formModelId) {
            $this->error('配置错误');
        }
        $headers = [
            'Content-type' => 'application/json',
            'X-Auth0-Token' => cache('qiqiaoToken'),
        ];
        $options = [
            'headers' => $headers,
        ];

        $url = sprintf('https://qiqiao.do1.com.cn/plus/cgi-bin/open/applications/%s/form_models/%s?page=1&pageSize=50000', $train->applicationId, $train->formModelId);
        $client = new Client($options);
        $response = $client->get($url);
        $data = json_decode($response->getBody(), 1);
        if ($data['code'] != 0) {
            $this->error($data['msg']);
        }
        $this->success('', $data['data']);
    }

    /**
     * @Author LK
     * @ApiTitle (查询)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="data", type="array", required=true, sample="", description="查询条件")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function query()
    {
        $post = request()->post();
        extract($post);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $train = ModelTrain::get($id);
        if (!$train) {
            $this->error('查询失败');
        }
        if (!isset($data) || !$data) {
            $this->error('数据不能为空');
        }
        if (!$train->applicationId || !$train->formModelId) {
            $this->error('配置错误');
        }
        $headers = [
            'Content-type' => 'application/json',
            'X-Auth0-Token' => cache('qiqiaoToken'),
        ];
        $options = [
            'headers' => $headers,
        ];
        $url = sprintf('https://qiqiao.do1.com.cn/plus/cgi-bin/open/applications/%s/forms/%s/query?page=1&pageSize=50000', $train->applicationId, $train->formModelId);
        $client = new Client($options);
        $response = $client->post($url, ['json' => $data]);
        $data = json_decode($response->getBody(), 1);
        if ($data['code'] != 0) {
            $this->error($data['msg']);
        }
        $this->success('', $data['data']);
    }
    // 7785968700701605889

    /**
     * @Author LK
     * @ApiTitle (表单查询)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="file_id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="filename", type="integer", required=true, sample="", description="文件名")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function download()
    {
        $param = request()->param();
        extract($param);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $train = ModelTrain::get($id);
        if (!$train) {
            $this->error('查询失败');
        }
        if (!$train->applicationId || !$train->formModelId) {
            $this->error('配置错误');
        }
        $headers = [
            'Content-type' => 'application/json',
            'X-Auth0-Token' => cache('qiqiaoToken'),
        ];
        $options = [
            'headers' => $headers,
        ];

        $url = sprintf('https://qiqiao.do1.com.cn/plus/cgi-bin/open/file_download/applications/%s/files/%s?page=1&pageSize=50000', $train->applicationId, $file_id);
        $client = new Client($options);
        $response = $client->get($url, ['stream' => true]);
        $root_path = ROOT_PATH . '/public/uploads/file/';

        if (!is_dir($root_path)) {
            @mkdir($root_path, 0755, true);
        }
        $filename=str_replace(' ','',$filename);
        $path = $root_path. $filename;
        $file = fopen($path, 'w');
        fwrite($file, $response->getBody());
        fclose($file);
        $this->success('', cdnurl('/uploads/file/'.$filename,true));

        
    }
}
